﻿/// <summary>
/// Developer: CHOOI KAH WAI
/// Last Modified: 22/11/2009
/// </summary>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Windows.Media.Media3D;
using System.Xml;
using System.Xml.Linq;
using ARSS.Editor.Common;
using System.Collections.ObjectModel;

namespace ARSS.Common
{
    /// <summary>
    /// Scene data
    /// </summary>
    class SceneDef
    {
        #region Properties

        /// <summary>
        /// Scene title
        /// </summary>
        public string Title { get; set; }

        /// <summary>
        /// Marker Board file path
        /// </summary>
        public string BoardPath { get; set; }

        /// <summary>
        /// Action marker file path
        /// </summary>
        public string ActionsPath { get; set; }

        /// <summary>
        /// Root scene node
        /// </summary>
        public SceneNode Root { get; set; }

        /// <summary>
        /// Action marker nodes
        /// </summary>
        public ArrayList Actions { get; set; }

        /// <summary>
        /// Animation nodes
        /// </summary>
        public Dictionary<string, ObservableCollection<KeyFrame>> Animations { get; set; }
        

        #endregion

        #region Functions

        /// <summary>
        /// Constructor
        /// </summary>
        public SceneDef()
        {
            Title = "";
            BoardPath = "";
            ActionsPath = "";
            Root = null;
            Actions = new ArrayList();
            Animations = new Dictionary<string, ObservableCollection<KeyFrame>>();
        }

        /// <summary>
        /// Load Scene
        /// </summary>
        /// <param name="_xml">The scene data file</param>
        public void LoadScene(XmlReader xmlFile)
        {
            // Load Scene Data
            XElement components = XElement.Load(xmlFile);
            BoardPath = components.Attribute("boardPath").Value;
            ActionsPath = components.Attribute("actionsPath").Value;
            Title = components.Attribute("title").Value;

            XElement rootElement = components.Element("Root");

            // Load root model

            Root = new SceneNode();
            Root.LoadNode(rootElement);


            // Load actions
            Actions.Clear();
            /*foreach (XElement ele in components.Element("Actions").Elements("Act"))
            {
                ActSceneNode node = new ActSceneNode();
                node.LoadNode(ele);
                Actions.Add(node);
            }*/

            // Load animations
            Animations.Clear();
            foreach (XElement ele in components.Element("Animations").Elements("Set"))
            {
                ObservableCollection<KeyFrame> ary = new ObservableCollection<KeyFrame>();
                foreach (XElement eleKeyFrame in ele.Elements("KeyFrame"))
                {
                    KeyFrame kf = new KeyFrame();
                    kf.LoadNode(eleKeyFrame);
                    ary.Add(kf);
                }
                Animations.Add(ele.Attribute("name").Value, ary);
            }

            //Logger.Instance.Log(string.Format("Scene loaded. SceneNodes #{0} ActionNodes #{1} AnimationNodes #{2}", Root.Children.Count + 1, Actions.Count, Animations.Count));
        }

        #endregion
    }
}
